<?php
namespace Test;

use Closure;
use Dybasedev\Dataman\Capsule\Manager as DB;
use Illuminate\Support\Fluent;

class ConvertSchool extends Convert
{
    public static $store;

    public function convert()
    {
        // 在事务内进行所有操作
        printf("  Start a database transaction.\n");
        DB::transaction(function () {
            // 取出学校的旧数据
            $schools = $this->connection->table('yuanbao_v1.t_kindergarten')->where('isEnable', '=', 1)->get();

            // 遍历学校，并抓取数据
            foreach ($schools as $school) {
                // 清理数据
                printf("  Clear convert cache.\n");
                ConvertSchool::$store = null;

                // 添加至DOMAIN
                printf("  Convert old school data and add to new database.\n");
                $spaceid = Tester::addDomain($this->connection, array('space' => 2, 'domain' => $school->name, 'created_time' => time(), 'update_time' => 0));
                printf("  New school domain(space id) is [%d]\n", $spaceid);

                // 从旧数据里提取校长信息并进行转换添加
                printf("  Add the school's president into the space [%d]\n", $spaceid);
                $schoolMaster = $this->connection->table('yuanbao_v1.t_user')->where('username', '=', $school->directorCode)->first();
                $data = array(
                    'name' => $schoolMaster->name,
                    'user' => $schoolMaster->username,
                    'password' => $schoolMaster->password,
                    'login_count' => $schoolMaster->loginNum ?: 0,
                    'created_time' => strtotime($schoolMaster->createDate),
                    'update_time' => empty($schoolMaster->modifyDate) ? 0 : strtotime($schoolMaster->modifyDate),
                    'nickname' => $schoolMaster->nickName ?: '',
                    'avatar' => $schoolMaster->img ?: '',
                    'telphone' => $schoolMaster->mobile ?: '',
                    'status' => $schoolMaster->status,
                    'email' => $schoolMaster->email ?: '',
                    'space' => $spaceid,
                    'chat_id' => $schoolMaster->username
                );

                $append = array(
                    'group' => array(
                        array('space' => $spaceid, 'group' => '校长', 'created_time' => time(), 'select' => true),
                        array('space' => $spaceid, 'group' => '老师', 'created_time' => time()),
                        array('space' => $spaceid, 'group' => '家长', 'created_time' => time()),
                        array('space' => $spaceid, 'group' => '医生', 'created_time' => time())
                    )
                );

                // $userid = $userAddTrigger($connection, $data, $append);
                // 添加校长账号
                $groupinfo = [];
                $userid = Tester::addUser($this->connection, $data, $append, function ($record) use (&$groupinfo) {
                    printf("\t* Add user group (or role) into the school manager data.\n");
                    $groupinfo[] = $record;
                });

                printf("  Ready to transform school base information\n");
                // 添加学校信息
                $convertResult = array(
                    'name' => $school->name,
                    'created_time' => strtotime($school->createDate),
                    'update_time' => empty($school->modifyDate) ? 0 : strtotime($school->modifyDate),
                    'telphone' => '',
                    'logo' => '',
                    'address' => '',
                    'sort' => 0,
                    'status' => 1,
                    'type' => 0,
                    'space' => $spaceid,
                    'master_id' => $userid
                );

                $schoolId = $this->connection->table('convert.v2_school')->insertGetId($convertResult);
                printf("  New school id is [%d], and transform school data.\n", $schoolId);
                printf("  Add school default role like teacher and parent.\n");
                $defaults = ['school_id' => $schoolId];
                foreach ($groupinfo as $key => $value) {
                    printf("  Convert role data: %d\n", $value['id']);
                    switch ($value['group']) {
                        case '家长':
                            $defaults['parent_role'] = $value['id'];
                            break;
                        case '老师':
                            $defaults['teacher_role'] = $value['id'];
                            break;
                    }
                }
                $this->connection->table('convert.v2_school_default_role')->insert($defaults);

                (new ConvertClass($this->connection))->convert($school, $schoolId, $groupinfo, $spaceid);
            }

        });
    }
}